<template>
  <div class="home" style="margin-left: 5px;width: auto">
    <!--    搜索区域-->
    <div style="margin: 10px">
      <el-input v-model="search" placeholder="请输入要搜索的菜品名称" style="width: 20%" clearable/>
      <el-select v-model="search_type" class="m-2" style="margin-left: 10px" @change="searchType" clearable placeholder="按菜品类型查找">
        <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.name"
            :value="item.id"
        />
      </el-select>
      <el-button type="primary" style="margin-left: 5px" @click="pageList">搜索</el-button>
    </div>
    <!--    功能区域-->
    <div style="margin: 10px">
      <el-button type="primary" @click="add">新增菜品</el-button>
      <el-popconfirm title="确认删除吗?" @confirm="deleteByIds">
        <template #reference>
          <el-button type="danger">批量删除</el-button>
        </template>
      </el-popconfirm>
    </div>
    <!--    数据区域-->
    <el-table :data="tableData" @selection-change="handleSelectionChange" border stripe style="width: calc(100vw - 205px)">
      <el-table-column type="selection" width="55" />
      <el-table-column :resizable="false" prop="id" label="id" />
      <el-table-column :resizable="false" prop="name" label="菜品名称" />
      <el-table-column :resizable="false" prop="price" label="菜品价格" />
      <el-table-column :resizable="false" prop="sum" label="菜品数量" />
      <el-table-column :resizable="false" prop="description" label="菜品描述" />
      <el-table-column :resizable="false" prop="type" label="菜品类型" />
      <el-table-column :resizable="false" label="菜品图片">
        <template #default="scope">
          <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.img"
              :preview-src-list="scope.row.img"
              :initial-index="4"
              fit="cover"
          />
        </template>
      </el-table-column>

      <el-table-column :resizable="false" label="上架/下架">
        <template #default="scope">
          <el-tooltip :content="scope.row.status==0?'上架':'下架'" placement="top">
            <el-switch
                v-model="scope.row.status"
                active-color="#13ce66"
                inactive-color="#ff4949"
                @change="changeStatus(scope.row)"
                active-value="0"
                inactive-value="1"
            />
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="165px">
        <template #default="scope">
          <!--          修改按钮-->
          <el-button @click="handleEdit(scope.row)">修改</el-button>
          <!--          删除按钮-->
          <el-popconfirm title="确认删除吗?" @confirm="handleDelet(scope.row.id)">
            <template #reference>
              <el-button type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--    分页插件-->
    <div style="margin: 10px">
      <el-pagination
          :currentPage="currentPage"
          :page-size="pageSize"
          :page-sizes="[5, 9]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>
    <!--    新增的模态窗口-->
    <el-dialog
        v-model="dialogVisible"
        title="新增菜品"
        width="30%"
    >
      <!--      窗口内的表单-->
      <el-form :model="form" label-width="120px">
        <el-form-item label="菜品名称">
          <el-input style="width: 80%" v-model="form.name" />
        </el-form-item>
        <el-form-item label="菜品价格">
          <el-input style="width: 80%" v-model="form.price" />
        </el-form-item>
        <el-form-item label="菜品数量">
          <el-input style="width: 80%" v-model="form.sum" />
        </el-form-item>
        <el-form-item label="菜品描述">
          <el-input type="textarea" style="width: 80%" v-model="form.description" />
        </el-form-item>
        <el-form-item label="菜品类型">
          <el-select v-model="form.type" class="m-2" :popper-append-to-body="false" placeholder="选择种类" size="large" >
            <el-option
                v-for="item in selectType"
                :key="item.id"
                :label="item.name"
                :value="item.id"
            />
          </el-select>
        </el-form-item>


        <el-form-item label="上架/下架">
          <el-switch style="width: 80%" active-value="0" inactive-value="1" v-model="form.status" />
        </el-form-item>
        <el-form-item label="菜品图片">
          <el-upload ref="upload" action="http://localhost:9001/order/files/upload/" :on-success="sucLoad">
            <el-button type="primary">上传图片</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <!--新增的模态窗口的确认按钮-->
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确认</el-button>
      </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
// @ is an alias to /src
import request from "@/utils/request";

export default {
  name: 'HomeView',
  components: {
    //注册

  },
  data() {
    return {
      form:{},
      dialogVisible:false,
      search:'',
      total:0,
      currentPage:1,
      pageSize:4,
      tableData:[],
      selectType:[],
      ids:[],
      search_type:'',
      options:[],
      type:''
    }
  },
  created() {
    this.pageList()
    this.getOrderType()
  },
  methods:{
    //按状态查询
    searchType(val) {
      request.get("order/food/searchType", {
        params: {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          type: val
        }
      }).then(res =>{
        this.type=val
        this.tableData = res.data
        // this.total = res.data.total
        // this.total=2
      })
    },
    deleteByIds() {
      console.log(this.ids)
      if(!this.ids.length){
        this.$message.error("请选择要删除的数据!");
        return
      }
      //批量删除
      request.post("order/food/deleteByIds/",this.ids).then(res => {
        this.msgTip("删除",res);
        this.pageList();
      })
    },
    handleSelectionChange(val) {
      //选中的数据
      this.ids=val.map(v => v.id);
    },
    getOrderType() {
      //获得菜品的种类
      request.get("order/type/getOrderType").then(res =>{
        this.selectType = res.data
        this.options = res.data
      });
    },
    changeStatus(row) {
      //改变菜品的上架下架状态
      request.post("order/food/changeStatus", {"status":row.status,"id":row.id}).then(res =>{
        if(row.status == 0){
          this.msgTip("上架",res);
        }else{
          this.msgTip("下架",res);
        }
      });
    },
    sucLoad(response) {
      //图片上传成功做的操作,1.访问上传的url返回response.data是图片地址2.赋值给表单的img元素
      this.form.img=response.data
    },
    pageList(){
      request.get("order/food/pageList", {
        params: {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          name: this.search,
          type: this.type
        }
      }).then(res =>{
        this.tableData = res.data.list
        this.total = res.data.total
      })
    },
    save(){
      //新增菜品
      if(this.form.id == null){
        request.post("order/food/save", this.form).then(res =>{
          this.msgTip("新增",res);
        });
      }else{
        //修改菜品
        request.put("order/food/save", this.form).then(res =>{
          this.msgTip("修改",res);
        });
      }
      //清空表单中已经上传的图片
      this.$refs.upload.clearFiles();
      this.pageList();
      this.dialogVisible=false;
    },
    add(){
      //打开新增模态窗口
      // console.log(this.selectType);
      this.getOrderType()
      this.dialogVisible = true;
      this.form = {};
    },
    handleEdit(row){
      //打开修改模态窗口
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
    },
    handleDelet(id){
      //删除
      request.delete("order/food/delete/" + id).then(res => {
        this.msgTip("删除",res);
        this.pageList();
      })
    },
    handleSizeChange(pageSize){
      this.pageSize=pageSize;
      this.pageList()
    },
    handleCurrentChange(currentPage){
      this.currentPage=currentPage;
      this.pageList()
    },
    msgTip(msg,res){
      if(res.code == 200){
        this.$message({
          type:"success",
          message: msg+"成功"
        })
      }else{
        this.$message({
          type:"error",
          message:res.msg
        })
      }
    }
  }
}
</script>

<style>
/*.el-popper {*/
/*  z-index: 100000 !important;*/
/*}*/
/*/deep/.el-select-dropdown {*/
/*  position: absolute !important;*/
/*  top: 50px !important;*/
/*  left: 0 !important;*/
/*}*/
</style>